// pages/organization/detail/index.js
import {
  BASE_URL
} from '../../../constant/index'
import {
  getRidingEventsByIdApi,
  putRidingEventsApi,
  postRidingEventsRegister,
  deleteRedingEventsApi,
  getRidingEventsApplyListApi
} from '../../../https/ridingEvents'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    detail: {},
    show: false,
    info: {},
    showUserList: false,
    userList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const docid = options.docid
    console.log(docid)
    const info = wx.getStorageSync('userData')
    console.log(info)
    this.setData({
      info
    })
    this.getRidingData(docid)
  },
  // 获取详情
  async getRidingData(id) {
    const getRes = await getRidingEventsByIdApi({
      id
    })
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        detail: getRes.data
      })
      this.getUserList()
    }

  },
  // 获取报名用户民单
  async getUserList() {
    const id = this.data.detail.id
    console.log(id)
    const getRes = await getRidingEventsApplyListApi({
      id
    })
    console.log(getRes)
    if (getRes.code == 200) {
      // const userList = getRes.data.map(item => item.participant)
      // const a = [...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data, ...getRes.data]
      this.setData({
        userList: getRes.data
      })
    }
  },
  // 切换状态
  bindSetting() {
    console.log(1)
  },
  onOpen(e) {
    this.setData({
      show: true
    })
  },
  onOpenUserList(e){
    this.setData({showUserList:true})
  },
  // 取消切换状态
  onClose() {
    this.setData({
      show: false,
      showUserList: false
    })
  },
  // 切换活动状态
  async toggleStatus(e) {
    const status = e.currentTarget.dataset.status
    console.log(status)
    console.log(this.data.detail)

    console.log(putRes)
    console.log(status)
    let content = '是否趣儿'
    if (status === 2) {
      content = '是否确认开始活动？'
    }
    if (status === 4) {
      content = '是否确认取消活动？'
    }
    if (status === 3) {
      content = '是否确认结束活动？'
    }
    const {
      confirm
    } = await wx.showModal({
      title: '提示',
      content: content,
    })
    if (!confirm) return
    wx.showLoading({
      title: '加载中..',
    })
    const id = this.data.detail.id
    const putRes = await putRidingEventsApi({
      id,
      status
    })

    if (putRes.code == 200) {
      await this.getRidingData(id)
      this.setData({
        show: false
      })
    }
    wx.hideLoading()
  },
  // 点击报名
  async bindApply() {
    const {
      cancel
    } = await wx.showModal({
      title: '提示',
      content: '是否确认报名活动？',
    })
    if (cancel) return
    wx.showLoading({
      title: '报名中..',
    })
    const id = this.data.detail.id
    console.log(id)
    console.log(id)
    const postRes = await postRidingEventsRegister({
      id
    })
    console.log(postRes)
    if (postRes.code == 200) {
      wx.showToast({
        title: postRes.message,
        icon: 'none'
      })
      this.getRidingData(id)
    } else {
      wx.showToast({
        title: postRes.message,
        icon: 'none'
      })
    }

  },
  // 删除活动
  async deleteRidding() {
    const {
      cancel
    } = await wx.showModal({
      title: '提示',
      content: '是否删除活动?',
    })
    if (cancel) return
    console.log(this.data.detail)
    const id = this.data.detail.id
    const delRes = await deleteRedingEventsApi({
      id
    })
    console.log(delRes)
    if (delRes.code == 200) {
      wx.showToast({
        title: '删除成功',
      })
      wx.navigateBack()
    } else {
      wx.showToast({
        title: '删除失败',
        icon: 'error'
      })
    }

  },
  // 跳转个人中心
  async gotoMyDetail(e) {
    console.log(e)
    const docid = e.currentTarget.dataset.docid
    wx.navigateTo({
      url: '../../my/detail/index?docid=' + docid,
    })
  },
  // 跳转聊天界面
  async gotoChatDetail(e) {
    const docid = e.currentTarget.dataset.docid

    wx.navigateTo({
      url: '../../message/chat/index?docid=' + docid,
    })
  }
})